//
//  CalendarViewController.m
//  PSYWeiBo
//
//  Created by xiaotian on 16/3/14.
//  Copyright © 2016年 郑亚田. All rights reserved.
//

#import "CalendarViewController.h"
#import <JTCalendar/JTCalendar.h>

@interface CalendarViewController () <JTCalendarDelegate>

@property (nonatomic,strong)JTCalendarManager *calendarMgr;
@property (nonatomic,strong)NSDate *todayDate;
@property (nonatomic,strong)NSDate *dateSelected;

@end

@implementation CalendarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLab.text = @"日历";
    
    self.calendarV = [CalendarView new];
    [self.view addSubview:self.calendarV];
    [self.calendarV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(@(CGRectGetMaxY(self.headerImgV.frame) - 20));
        make.top.mas_equalTo(self.headerImgV.mas_bottom);
//        make.top.equalTo().width.offset(-20);
        make.left.right.equalTo(@0);
        make.height.equalTo(@(kScreenHeight * .4));
    }];
    
    self.calendarMgr = [JTCalendarManager new];
    self.calendarMgr.delegate = self;
    [self.calendarMgr setMenuView:self.calendarV.calendarMenuV];
    [self.calendarMgr setContentView:self.calendarV.calendarContentV];
    self.todayDate = [NSDate date];
    [self.calendarMgr setDate:[NSDate date]];
    self.dateSelected = [NSDate date];
    [self.calendarMgr reload];
}

#pragma mark - jtcalendar代理方法
- (void)calendar:(JTCalendarManager *)calendar prepareDayView:(JTCalendarDayView *)dayView {
    //当天
    if ([self.calendarMgr.dateHelper date:[NSDate date] isTheSameDayThan:dayView.date]) {
        dayView.circleView.hidden = NO;
        dayView.textLabel.text = @"今";
        dayView.circleView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:103 / 255.0 blue:119 / 255.0 alpha:1.0];
        dayView.textLabel.textColor = [UIColor whiteColor];
    }else if (self.dateSelected && [self.calendarMgr.dateHelper date:self.dateSelected isTheSameDayThan:dayView.date]) {
        //点击的哪天
        dayView.circleView.hidden = NO;
        dayView.circleView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:103 / 255.0 blue:119 / 255.0 alpha:.5];
        dayView.textLabel.textColor = [UIColor whiteColor];
    }else if (![self.calendarMgr.dateHelper date:self.calendarV.calendarContentV.date isTheSameMonthThan:dayView.date]) {
        //不属于当月的
        dayView.circleView.hidden = YES;
        dayView.textLabel.textColor = [UIColor lightGrayColor];
    }else {
        //属于当月但是没有点击
        dayView.circleView.hidden = YES;
        dayView.textLabel.textColor = [UIColor colorWithRed:169 / 255.0 green:121 / 255.0 blue:65 / 255.0 alpha:1.0];
    }
}

- (void)calendar:(JTCalendarManager *)calendar didTouchDayView:(JTCalendarDayView *)dayView {
    self.dateSelected = dayView.date;
    dayView.circleView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
    [UIView transitionWithView:dayView duration:.3 options:0 animations:^{
        dayView.circleView.transform = CGAffineTransformIdentity;
        [self.calendarMgr reload];
    } completion:nil];
    
    if (![self.calendarMgr.dateHelper date:self.calendarV.calendarContentV.date isTheSameMonthThan:dayView.date]) {
        if ([self.calendarV.calendarContentV.date compare:dayView.date] == NSOrderedAscending) {
            [self.calendarV.calendarContentV loadNextPageWithAnimation];
        }else {
            [self.calendarV.calendarContentV loadPreviousPageWithAnimation];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

